﻿Imports System.ComponentModel

Public Class CustomComboBox
    Inherits ComboBox
    Enum Type
        Ville = 1
        Province = 2
        Pays = 3
    End Enum
    Dim LocalType As Type

    Dim oLocalContext As DBMainContext

    <Browsable(True), DefaultValue(1), Category("Behavior"), Description("Choisi quel element de la base de donnée retourner.")> _
    Public Property ElementAffiche() As Type
        Get
            Return LocalType
        End Get
        Set(ByVal value As Type)
            LocalType = value
        End Set
    End Property



    Private Sub VilleComboBox_Validated(sender As Object, e As System.EventArgs) Handles Me.Validated
        Dim strValue As String
        oLocalContext = New DBMainContext
        If Me.Text.Length > 2 AndAlso Validate(Me.Text) Then
            Select Case LocalType
                Case Type.Ville
                    If Not (From u In oLocalContext.Ville Select u.Nom).Contains(Me.Text) Then
                        Dim oVille As New Ville
                        Me.Text = Char.ToUpper(Me.Text(0)) + Me.Text.Substring(1)
                        oVille.Nom = Me.Text
                        oLocalContext.Ville.InsertOnSubmit(oVille)
                        oLocalContext.SubmitChanges()
                        strValue = Me.Text
                        Me.DataSource = Nothing
                        Load()
                        Me.SelectedText = oVille.Nom
                    End If
                Case Type.Province
                    If Not (From u In oLocalContext.Province Select u.Nom).Contains(Me.Text) Then
                        Dim oProvince As New Province
                        Me.Text = Char.ToUpper(Me.Text(0)) + Me.Text.Substring(1)
                        oProvince.Nom = Me.Text
                        oLocalContext.Province.InsertOnSubmit(oProvince)
                        oLocalContext.SubmitChanges()
                        strValue = Me.Text
                        Me.DataSource = Nothing
                        Load()
                        Me.SelectedText = oProvince.Nom
                    End If
                Case Type.Pays
                    If Not (From u In oLocalContext.Pays Select u.Nom).Contains(Me.Text) Then
                        Dim oPays As New Pays
                        Me.Text = Char.ToUpper(Me.Text(0)) + Me.Text.Substring(1)
                        oPays.Nom = Me.Text
                        oLocalContext.Pays.InsertOnSubmit(oPays)
                        oLocalContext.SubmitChanges()
                        strValue = Me.Text
                        Me.DataSource = Nothing
                        Load()
                        Me.SelectedText = oPays.Nom
                    End If
            End Select
        End If
    End Sub

    Public Sub Load()
        oLocalContext = New DBMainContext
        Select Case LocalType
            Case Type.Ville
                Me.DataSource = oLocalContext.Ville.OrderBy(Function(Ville) Ville.Nom)
                Me.DisplayMember = "Nom"
                Me.SelectedIndex = -1
            Case (Type.Province)
                Me.DataSource = oLocalContext.Province.OrderBy(Function(Province) Province.Nom)
                Me.DisplayMember = "Nom"
                Me.SelectedIndex = -1
            Case Type.Pays
                Me.DataSource = oLocalContext.Pays.OrderBy(Function(Pays) Pays.Nom)
                Me.DisplayMember = "Nom"
                Me.SelectedIndex = -1
        End Select

        Me.MaxLength = 50
    End Sub

    Private Function Validate(ByVal strNom As String) As Boolean
        If Outils.Validation.CheckRegex(Outils.Validation.TypeRegex.Nom, strNom) Then
            Return True
        Else
            Me.Text = ""
        End If

    End Function

End Class
